﻿using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlServerCe;

namespace RFMobile.Database
{
    public class DatabaseManager
    {
        private String connectionString = ("Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\\Database\\rfmantencion.sdf;"));
        private SqlCeConnection conn;
        private SqlCeCommand command;
        private SqlCeCommandBuilder builder;
        private DataTable dt;

        private DataSet ds;
        private SqlCeDataAdapter da = null;

        public DatabaseManager()
        {
            conn = new SqlCeConnection();
            conn.ConnectionString = connectionString;
            conn.Open();
        }

        public Boolean Query(String str)
        {
            try
            {
                command = new SqlCeCommand(str, conn);
                if (command.ExecuteScalar() != null)
                {
                    return (true);
                }
                else
                {
                    throw new ObjectDisposedException(command.ToString(), "Query problems...");
                }
            }
            catch (ObjectDisposedException ode)
            {
                return (false);
            }
        }

        public SqlCeConnection Conn
        {
            set { this.conn = value; }
            get { return this.conn; }
        }

        public SqlCeCommand Command
        {
            set { this.command = value; }
            get { return this.command; }
        }

        public DataSet Ds
        {
            set { this.ds = value; }
            get { return this.ds; }
        }

        public SqlCeDataAdapter Da
        {
            set { this.da = value; }
            get { return this.da; }
        }

        public DataTable Dt
        {
            set { this.dt = value; }
            get { return this.dt; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="dsName"></param>
        public void BuildDataSet(String tableName, String dsName)
        {
            ds = new DataSet(dsName);
            da = new SqlCeDataAdapter("SELECT * FROM " + tableName, conn);
            builder = new SqlCeCommandBuilder(da);
            da.Fill(ds, tableName);
            dt = ds.Tables[tableName];
        }

        /// <summary>
        /// Construye DataSet para su posterior manipulación.
        /// </summary>
        /// <param name="arrStr">Array de tablas para construir DataSet.</param>
        /// <param name="dsName">Nombre del DataSet.</param>
        public void BuildDataSet(String[] arrStr, String dsName)
        {
            ds = new DataSet(dsName);
            foreach (String tableName in arrStr)
            {
                if (da == null)
                {
                    da = new SqlCeDataAdapter("SELECT * FROM " + tableName, conn);
                }
                else
                {
                    da.SelectCommand.CommandText = "SELECT * FROM " + tableName;
                }
                builder = new SqlCeCommandBuilder(da);
                da.FillSchema(ds, SchemaType.Source, tableName);
                da.Fill(ds, tableName);
            }
        }

        public void BuildDataSet(string dsName)
        {
            ds = new DataSet(dsName);
        }

        public void Close()
        {
            if (command != null)
            {
                command.Dispose();
            }
            
            if (da != null)
            {
                da.Dispose();
            }

            if (ds != null)
            {
                ds.Dispose();
            }

            conn.Close();
            conn.Dispose();
        }
    }
}
